package com.chinasoft.mall.activity.adapter;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.model.response.GoodsByClassEmployeeBean;

public class GoodsEmployeeAdapter extends BaseAdapter {

	private List<GoodsByClassEmployeeBean> dds;
	private LayoutInflater inflater;
	private ViewHolder _vh;
	
	private static final String IMAGE_URL = "http://m.hao24.cn/mobile/egoods_class_img/";
	
	private int[] images = { R.drawable.class_1, R.drawable.class_2,
			R.drawable.class_3, R.drawable.class_4, R.drawable.class_5,
			R.drawable.class_6, R.drawable.class_7, R.drawable.class_8,
			R.drawable.class_9, R.drawable.class_10, R.drawable.class_11, };

	public GoodsEmployeeAdapter(MainActivity MainActivity, List<GoodsByClassEmployeeBean> crs) {
		inflater = LayoutInflater.from(MainActivity);
		this.dds = crs;
	}

	public void setData(List<GoodsByClassEmployeeBean> crs) {
		this.dds = crs;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (dds == null) {
			return 0;
		} else {
			return dds.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (dds == null) {
			return null;
		} else {
			return dds.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static Bitmap getHttpBitmap(String url){
    	URL myFileURL;
    	Bitmap bitmap=null;
    	try{
    		myFileURL = new URL(url);
    		//获得连接
    		HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
    		//设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
    		conn.setConnectTimeout(6000);
    		//连接设置获得数据流
    		conn.setDoInput(true);
    		//不使用缓存
    		conn.setUseCaches(false);
    		//这句可有可无，没有影响
    		//conn.connect();
    		//得到数据流
    		InputStream is = conn.getInputStream();
    		//解析得到图片
    		bitmap = BitmapFactory.decodeStream(is);
    		//关闭数据流
    		is.close();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
		return bitmap;
    	
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.splv, null);
			_vh = new ViewHolder();
			_vh.splv = (TextView) convertView.findViewById(R.id.splv);
			_vh.spimg = (ImageView) convertView.findViewById(R.id.spimg);
			convertView.setTag(_vh);
		} else {
			_vh = (ViewHolder) convertView.getTag();
		}
		GoodsByClassEmployeeBean _dd = dds.get(position);
		_vh.splv.setText(_dd.getCLASS_NAME());
		if (dds.size() > position) {
			String url = IMAGE_URL + _dd.getCLASS_ICON();
	        //得到可用的图片
	        Bitmap bitmap = getHttpBitmap(url);
			_vh.spimg.setImageBitmap(bitmap);
		}
		return convertView;
	}


	class ViewHolder {
		public TextView splv;
		public ImageView spimg;
	}
}
